<template>
	<picker @change="pickerChange" @columnchange="columnchange" :range="array" range-key="n" :value="value" mode="multiSelector">
		<slot></slot>
	</picker>
</template>

<script>
	import AllAddress from './data.js'
	import request from '../../common/common.js'
	export default {
		data() {
			return{
				value: [],
				array: [],
				index: 0,
				api:request.api,
			}
		},
		props:{
			level:{ //级数
				type: Number,
				default: 3
			}
		},
		created() {
			this.init()
		},
		methods:{
			// 初始化地址选项
			init() {
				let that = this
				uni.request({
					url: this.api+'/api/district/list',
					method:  'GET',
					data: {},
					header:{'content-type':'application/json; charset=utf-8'},
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200){
							// let newArr = []
							// res.data.data.forEach((item,index)=>{
								
							// })
							that.array = new Array(that.level);
							for(let i = 0; i<that.array.length;i++){
								if(i==0){
									that.array[i] = res.data.data
								} else {
									that.array[i] = [];
									if (that.array[i-1][0].c != null) {
										that.array[i] = that.array[i-1][0].c;
									}
								}
								
							}
							that.$forceUpdate()
						}
							
					}
				})
				
			},
			// 地址控件改变控件
			columnchange(e) {
				let aIndex = e.detail.column+1;
				let j = e.detail.value;
				for(let i=aIndex; i<this.array.length; i++){
					this.array[i] = [];
					if (this.array[i-1][j].c != null) {
						this.array[i] = this.array[i-1][j].c;
					}
				}
				this.$forceUpdate();
			},
			
			/**
			 * 点击确定
			 * */
			pickerChange(e) {
				var result =  [];
				// console.log(e)
				for(let i=0; i<this.array.length; i++){
					// console.log(this.array[i][e.target.value[i]])
					result.push({
						name: this.array[i][e.target.value[i]].n,
						id: this.array[i][e.target.value[i]].v
					})
				};
				this.$emit('change', {
					data: result
				})
			}
			
		}
	}
</script>

<style>
</style>
